// vote-info.js
const util = require('../../utils/util.js')
const appConfig = require('../../config.js')
const app = getApp()

Page({
  data: {
    isLoading: true,
    choseVote: [],
    voteId: 0,
    isCanVote: false,
    useWxAuth: false,
    voteData: {
      title: '',
      desc: '',
      myChoice: [], // 我的选择
      choices: [],
      author: '',
      isMult: false,
      joinUserNum: 0, // 总参与人数
      createTime: '', // 创建时间
      endTime: '' // 截止时间
    }
  },
  // 分享配置
  onShareAppMessage: function (res) {
    var url = 'pages/vote-info/vote-info?voteId=' + this.data.voteId
    return {
      title: '投票',
      path: url
    }
  },
  // 选择投票
  vote: function (event) {
    if (!this.data.isCanVote) {
      return false
    }
    var voteItemIndex = event.currentTarget.dataset.index
    var voteData = this.data.voteData
    var userChose = this.data.choseVote

    // 如果已经选中过，触发取消
    if (userChose.includes(voteItemIndex)) {
      var index = userChose.indexOf(voteItemIndex)
      userChose.splice(index, 1)
    } else {
      // 单选，清空
      if (!voteData.isMult) {
        userChose = []
      }
      userChose.push(voteItemIndex)
    }

    this.updateVoteStatus(userChose)
  },
  // 更新选择状态
  updateVoteStatus (userChose) {
    var voteData = this.data.voteData
    // 是否能投票
    if (voteData.myChoice && voteData.myChoice.length) {
      this.data.isCanVote = false
    } else {
      this.data.isCanVote = true
    }

    // 更新选项选中状态
    voteData.choices.forEach((item, index) => {
      if (userChose.includes(index)) {
        item.isSelected = true
      } else {
        item.isSelected = false
      }
    })

    this.setData({
      choseVote: userChose
    })

    this.setData({
      voteData: voteData
    })

    this.setData({
      isCanVote: this.data.isCanVote
    })
  },
  // 保存投票结果
  saveMyChoice: function () {
    var that = this
    var voteData = this.data.voteData
    var userChose = this.data.choseVote
    if (!userChose.length) {
      wx.showToast({
        title: '请选择！',
        icon: 'none',
        duration: 3000
      })
      return false
    }

    wx.showLoading()
    app.requestWithCredentials({
      url: appConfig.APP_URL_PREFIX + 'choose/',
      method: "POST",
      data: {
        id: this.data.voteId,
        choices: userChose,
      },
      success: (res) => {
        wx.hideLoading()
        if (res.data.code === 0) {
          wx.showToast({
            icon: 'success'
          })
          this.initVoteDetail(that.data.voteId)
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.message
          })
        }
      }
    })
  },
  // 加载投票详情
  initVoteDetail: function (voteId) {
    var that = this
    wx.showLoading()
    app.requestWithCredentials({
      url: appConfig.APP_URL_PREFIX + 'vote_detail/',
      method: "GET",
      data: {
        id: voteId
      },
      success: (res) => {
        console.log('res', res)
        if (res.data.code === 0) {
          var data = res.data.data
          var userChose = []
          data.choices.forEach(item => {
            item.isSelected = false
          })
          this.data.voteData = data
          data.myChoice.forEach(item => {
            userChose.push(item)
          })
          this.updateVoteStatus(userChose)
        }
        wx.hideLoading()
        this.setData({
          isLoading: false
        })
      }
    })
  },
  userInfoSuccessSetCallback: function () {
    this.hideDialog();
    this.initVoteDetail(this.data.voteId);
  },
  onLoad: function (options) {
    wx.hideLoading();
    this.data.voteId = options.voteId;
    if (!app.globalData.userInfo) {
      this.setData({
        useWxAuth: true
      })
    } else {
      this.initVoteDetail(this.data.voteId);
    }
  }
})
